
var newer = newer || {};
newer.gd = newer.gd || {};

newer.IdentityDiv = function(o) {
	this.domNode = o.domNode;
	
	this.init();
};

newer.IdentityDiv.prototype = {
	init: function() {
		$("#identity-div").html("");
		$("#identity-div").append('<div id="identity-avatar-div"></div>');
		$("#identity-div").append('<div id="identity-name-div"></div>');
		
		this.domNode.removeClass("nw-event-listener").addClass("nw-event-listener");
		this.domNode.off("nw-event").on("nw-event", $.proxy(this.nweHandler,this));
		
		this.refresh();
	},
	
	refresh: function() {
		this.update();
	},
	
	nweHandler: function(e,c) {
		if(c.type == newer.EventHelper.AUTH_TYPE) {
			this.updateAvatar().updateName();
		}
	},
	
	updateAvatar: function() {
		var self = this;
		if(newer.gd.user.hasPhoto==true) {
			$("img#avatar-img").attr('src', "/Photos/displayUserPhoto?id="+newer.gd.user.id+"&s="+Math.random());
		} else {
			$("img#avatar-img").attr('src', "/public/images/default-user-image");
		}
		$("img#avatar-img").on("click", {pid:newer.gd.user.id, self:self},function(event) {
			var fromStatus = newer.gd.pstatus;
			var toStatus = {
				ptype:newer.UriHelper.PEOPLE_TYPE,
				id:newer.gd.user.id,
				tab:"about"
			};
			newer.UriHelper.initRedirection({
				fromStatus:fromStatus,
				toStatus:toStatus
			});
			$(".nw-event-listener").trigger("nw-event", {
				type:newer.EventHelper.PAGE_TYPE, 
				fromStatus:fromStatus,
				toStatus:toStatus
			});
		}).addClass("link-like-element");
		return this;
	},
	
	updateName: function() {
		var self = this;
		$("#identity-name-div").html(newer.gd.user.name);
		$("#identity-name-div").on("click", {pid:newer.gd.user.id, self:self},newer.PeopleHelper.gotoPeopleHandler).addClass("link-like-element");
		return this;
	},
	
	update: function() {
		$("#identity-avatar-div").html('<img id="avatar-img" width=60px/>');
		this.updateAvatar().updateName();
	}
};


